package com.saxon.blog.controller;


import com.saxon.blog.pojo.User;
import com.saxon.blog.service.user.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Controller
public class UserController {
    private UserServiceImpl userService;

    @Autowired
    public void setUserService (UserServiceImpl userService) {
        this.userService = userService;
    }

    @ApiOperation ("注册用户")
    @GetMapping ("/register")
    public String register (String userName, String userAccount, String userPassword) {
        User user = new User ();
        user.setUserName (userName);
        user.setUserRole (2);
        user.setUserAccount (userAccount);
        user.setUserPassword (userPassword);
        user.setCreateDate (new Date ());
        user.setModifyDate (new Date ());
        int i = userService.insertUser (user);
        if (i == 0) {
            return "register";
        }
        return "index";

    }

    @ApiOperation ("对用户名进行验证")
    @GetMapping ("/admin/register/username")
    @ResponseBody
    public String check_userName (String userName) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("userName", userName);
        if (userName != "") {
            if (userService.getUser (map) == null) {
                return "false";
            } else {
                return "error";
            }

        }
        return "true";
    }

    @ApiOperation ("对用户账户进行一个验证")
    @GetMapping ("/admin/register/userAccount")
    @ResponseBody
    public String check_userAccount (String userAccount) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("userAccount", userAccount);
        if (userAccount != "") {
            if (userService.getUser (map) == null) {
                return "false";
            } else {
                return "error";
            }

        }
        return "true";
    }

    @ApiOperation ("找回密码")
    @PostMapping ("/pwdmodify")
    public String pwdmodify (String userAccount, String newpassword) {
        int i = userService.updateUser (userAccount, newpassword);
        if (i == 0) {
            return "updatePassword";
        }
        return "index2";
    }

    @ApiOperation ("账户验证")
    @GetMapping ("/pwdmodify/account")
    @ResponseBody
    public String getAccount (String userAccount) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("userAccount", userAccount);
        if (userService.getUser (map) == null) {
            return "false";
        }
        return "true";
    }

    @ApiOperation ("密码验证")
    @RequestMapping ("/pwdmodify/newpassword")
    @ResponseBody
    public String getNewpassword (@RequestParam ("newpassword") String newpassword) {
        if (newpassword != null && newpassword != "") {
            if (newpassword.length () > 6 && newpassword.length () < 20) {
                return "true";
            }
        }
        return "false";

    }

    @ApiOperation ("再次确认密码")
    @RequestMapping ("/pwdmodify/rnewpassword")
    @ResponseBody
    public String getRnewpassword (@RequestParam ("newpassword") String newpassword, @RequestParam ("rnewpassword") String rnewpassword) {
        if (rnewpassword != null && rnewpassword != "") {
            if (newpassword != null && newpassword != "") {
                if (rnewpassword.length () > 6 && rnewpassword.equals (newpassword) && rnewpassword.length () < 20) {
                    return "true";
                }
            }
        }
        return "false";
    }

    @ApiOperation ("用户登录")
    @RequestMapping ("/admin/getUser")
    @ResponseBody
    public List<User> getUserWithList (String userName) {
        Map<String, Object> map = new HashMap<> ();
        if (userName != null && ! userName.equals ("")) {
            if (userName.matches ("[\\d]+")) {
                map.put ("userAccount", userName);
            } else {
                userName = "%" + userName + "%";
                map.put ("userName", userName);
            }
        }
        return userService.getUserWithList (map);
    }

    @ApiOperation ("查看用户总数")
    @GetMapping ("/admin/queryUserNum")
    @ResponseBody
    public int queryUser () {
        return userService.queryNum ();
    }

    @ApiOperation ("删除用户")
    @GetMapping ("/admin/deleteUser")
    @ResponseBody
    public int deleteUserById (String id) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("id", Integer.parseInt (id));
        return userService.deleteUserById (map);
    }

    @ApiOperation ("添加用户")
    @PostMapping ("/admin/addUser")
    @ResponseBody
    public boolean addUser (String userName, String userAccount, String userPassword) {
        User user = new User ();
        user.setUserName (userName);
        user.setUserRole (2);
        user.setUserAccount (userAccount);
        user.setUserPassword (userPassword);
        user.setCreateDate (new Date ());
        user.setModifyDate (new Date ());
        int i = userService.insertUser (user);
        return i == 0;
    }

    @ApiOperation ("使用用户id删除用户多选")
    @GetMapping ("/admin/deleteUserByIds")
    @ResponseBody
    public boolean deleteUserByIds (String[] checked) {
        int a = 0;
        if (checked != null) {
            for (int i = 1; i < checked.length - 1; i++) {
                Map<String, Object> map = new HashMap<> ();
                map.put ("id", Integer.parseInt (checked[i].split ("\"")[1]));
                int i1 = userService.deleteUserById (map);
                if (i1 > 0) {
                    a++;
                }
            }
        }
        return a > 0;
    }

    @ApiOperation ("根据用户id查询用户")
    @GetMapping ("/admin/getUserById")
    @ResponseBody
    public User getUserById (String id) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("id", id);
        return userService.getUser (map);
    }

    @ApiOperation ("更新用户信息")
    @GetMapping ("/admin/updateUserInfo")
    @ResponseBody
    public int updateUserInfo (String id, String userName, String userPassword, String userAccount, String inputState) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("id", id);
        map.put ("userName", userName);
        map.put ("userPassword", userPassword);
        map.put ("userAccount", userAccount);
        map.put ("userRole", inputState);
        return userService.updateUser (map);
    }
}
